//
//  KSChooseCodeView.swift
//  Supermarket
//
//  Created by Black_Crystal on 2017/9/27.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit

protocol ViewChooseCallBackDelegate: NSObjectProtocol {
    func viewChooseCallBack(selectRow: NSInteger)
}

class KSChooseCodeView: UIView {
    weak var delegate: ViewChooseCallBackDelegate?
    var dataArray: [String] = []
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        self.isHidden = true
    }
    
    func setViewALLtypeUI(width: CGFloat) {
        self.isHidden = false
        for view in self.subviews {
            view.removeFromSuperview()
        }
        
        let backView = UIView()
        backView.backgroundColor = UIColor.white
        self.addSubview(backView)
        backView.snp.makeConstraints { (make) in
            make.top.right.left.equalTo(0)
            make.height.equalTo((dataArray.count/2 + 1) * (40 + 10) + 10)
        }
        
        for i in 0...(dataArray.count - 1) {
            let space: CGFloat = 10
            let viewW: CGFloat = (width - CGFloat(space * 3))/2
            let viewH: CGFloat = 40
            let viewX: CGFloat = CGFloat(i % 2) * (viewW + space) + space
            let viewY: CGFloat = CGFloat(i / 2) * (viewH + space) + space
            
            let viewLabel = UILabel.init(frame: CGRect.init(x: viewX, y: viewY, width: viewW, height: viewH))
            viewLabel.layer.masksToBounds = true
            viewLabel.layer.cornerRadius = 5
            viewLabel.isUserInteractionEnabled = true
            viewLabel.tag = i + 10000
            viewLabel.textAlignment = .center
            viewLabel.backgroundColor = KSColor.kColor_line
            let attributed = NSMutableAttributedString()
            let word = NSAttributedString.init(string: dataArray[i], attributes: [.foregroundColor : KSColor.kColor_3,.font : UIFont.systemFont(ofSize: 15.0)])
            attributed.append(word)
            viewLabel.attributedText = attributed
            
            let tap = UITapGestureRecognizer()
            tap.addTarget(self, action: #selector(chooseEvent(tap:)))
            viewLabel.addGestureRecognizer(tap)
            
            backView.addSubview(viewLabel)
        }
    }
    
    func setViewRightUI(width: CGFloat) {
        self.isHidden = false
        for view in self.subviews {
            view.removeFromSuperview()
        }
        for i in 0...(dataArray.count - 1) {
            let viewW: CGFloat = width
            let viewH: CGFloat = 40
            let viewX: CGFloat = KSConstants.kScreenWidth - width
            let viewY: CGFloat = CGFloat(i) * viewH
            
            let viewBtn = UIButton.init(frame: CGRect.init(x: viewX, y: viewY, width: viewW, height: viewH))
            viewBtn.backgroundColor = UIColor.white
            viewBtn.setTitle(dataArray[i], for: .normal)
            viewBtn.setTitleColor(KSColor.kColor_9, for: .normal)
            viewBtn.setTitleColor(KSColor.kColorMainRed, for: .selected)
            viewBtn.addTarget(self, action: #selector(chooseEvent(button:)), for: .touchDown)
            self.addSubview(viewBtn)
        }
    }
    
    @objc func chooseEvent(tap: UITapGestureRecognizer) {
        let label = tap.view as! UILabel
        label.layer.borderWidth = 0.5
        label.layer.borderColor = KSColor.kColorMainRed.cgColor
        label.backgroundColor = UIColor.white
        self.isHidden = true
        self.delegate?.viewChooseCallBack(selectRow: label.tag - 10000)
    }
    
    @objc func chooseEvent(button: UIButton) {
       
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
